package kau.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.AttendInfo;

import org.apache.ibatis.session.SqlSession;

public class AttendInfoDAOImpl implements AttendInfoDAOIF {

	DBUtil db = null;
	private static AttendInfoDAOImpl instance;
	
	private AttendInfoDAOImpl(){
		db = DBUtil.getInstance();
	}
	
	public static AttendInfoDAOImpl getInstance(){
		if(instance == null) instance = new AttendInfoDAOImpl();
		return instance;
	}
	
	public boolean isExist(String custId, int courseId, String day){
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		
		HashMap info = new HashMap<>();
		info.put("custId", custId);
		info.put("courseId", courseId);
		info.put("day", day);
		try{
			//param = int classId, String custId, String day
			
			result = session.selectOne("dbConfig.AttendInfoMapper.isExist", info);
		}finally{
			if(session != null)
			session.close();
		}
		if(result!= null) flag= true;
		
		return flag;
		
	}
	@Override
	public void insert(AttendInfo attendanceInfo)
			throws DuplicationException {
		
		if(isExist(attendanceInfo.getCustId(),attendanceInfo.getCourseId(),attendanceInfo.getDay())) throw new DuplicationException(attendanceInfo);
		SqlSession session = db.getSession();
		try{
			session.insert("dbConfig.AttendInfoMapper.insert", attendanceInfo);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}
	}

	@Override
	public void update(AttendInfo attendanceInfo)
			throws RecordNotFoundException {
		
		
		if(!isExist(attendanceInfo.getCustId(),attendanceInfo.getCourseId(),attendanceInfo.getDay())) throw new RecordNotFoundException(attendanceInfo);
		SqlSession session = db.getSession();
		try{
			session.update("dbConfig.AttendInfoMapper.update", attendanceInfo);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}

	}

	@Override
	public void delete(String custId, int courseId, String day)
			throws RecordNotFoundException {
		if(!isExist(custId,courseId,day)) throw new RecordNotFoundException("정보가없습니다");
		
		HashMap info = new HashMap<>();
		info.put("custId", custId);
		info.put("courseId", courseId);
		info.put("day", day);
		
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.AttendInfoMapper.delete", info);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}
	}

	@Override
	public AttendInfo getAttendInfo(String custId, int courseId, String day)
			throws RecordNotFoundException {
		System.out.println("------------------------");
		
		AttendInfo attendanceInfo = new AttendInfo();
		attendanceInfo.setCustId(custId);
		attendanceInfo.setCourseId(courseId);
		attendanceInfo.setDay(day);
		System.out.println(attendanceInfo);
		//isExist
		if(!isExist(custId,courseId,day)) throw new RecordNotFoundException(attendanceInfo);

		SqlSession session = db.getSession();
		AttendInfo attendInfo = null;
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("custId", custId);
		param.put("courseId", courseId);
		param.put("day", day);
		
		
		try{
			attendInfo = session.selectOne("dbConfig.AttendInfoMapper.getAttendInfo", param);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}
		return attendInfo;
	}

	@Override
	public ArrayList<AttendInfo> getAttendInfoList(String custId, int courseId)
	{
		ArrayList<AttendInfo> attendInfoList = new ArrayList<AttendInfo>();
		SqlSession session = db.getSession();
		
		HashMap info = new HashMap<>();
		info.put("custId", custId);
		info.put("courseId", courseId);
		try{
			attendInfoList = (ArrayList) session.selectList("dbConfig.AttendInfoMapper.getAttendInfoList",info);
		} finally{
		if(session != null)
		session.close();
		}
		return attendInfoList;
		
	}

	@Override
	public ArrayList<AttendInfo> getAllAttendInfoList(int courseId, String day) {
		ArrayList<AttendInfo> attendInfoList = new ArrayList<AttendInfo>();
		SqlSession session = db.getSession();
		
		HashMap info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("day", day);
		try{
			attendInfoList = (ArrayList) session.selectList("dbConfig.AttendInfoMapper.getAllAttendInfoList",info);
		} finally{
		if(session != null)
		session.close();
		}
		return attendInfoList;
		
	}

	@Override
	public void periodDelete(String custId, int courseId,
			String startDay, String endDay) throws RecordNotFoundException {		
		HashMap info = new HashMap<>();
		info.put("custId", custId);
		info.put("courseId", courseId);
		info.put("startDay", startDay);
		info.put("endDay", endDay);
		
		
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.AttendInfoMapper.periodDelete", info);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}
	}

	@Override
	public void lowPeriodDelete(String custId, int courseId, String day) {
		// TODO Auto-generated method stub
		HashMap info = new HashMap<>();
		info.put("custId", custId);
		info.put("courseId", courseId);
		info.put("day", day);
		
		
		
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.AttendInfoMapper.lowPeriodDelete", info);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}
	}

	@Override
	public void highPeriodDelete(String custId, int courseId, String day) {
		// TODO Auto-generated method stub
		HashMap info = new HashMap<>();
		info.put("custId", custId);
		info.put("courseId", courseId);
		info.put("day", day);
		
		
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.AttendInfoMapper.highPeriodDelete", info);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}
	}

	@Override
	public ArrayList<AttendInfo> getPeriodicalAttendInfoList(int courseId,
			String startDate, String endDate) {
		ArrayList<AttendInfo> attendInfoList = new ArrayList<AttendInfo>();
		SqlSession session = db.getSession();
		
		HashMap info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("startDate", startDate);
		info.put("endDate", endDate);
		try{
			attendInfoList = (ArrayList) session.selectList("dbConfig.AttendInfoMapper.getPeriodicalAttendInfoList",info);
		} finally{
		if(session != null)
		session.close();
		}
		return attendInfoList;
	}

	@Override
	public List getPeriodicalDay(int courseId, String startDate,
			String endDate) {
		SqlSession session = db.getSession();
		List periodicalDayList = null;
		HashMap info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("startDate", startDate);
		info.put("endDate", endDate);
		System.out.println("getPeriodicalDay"+info);
		try{
			periodicalDayList = session.selectList("dbConfig.AttendInfoMapper.getPeriodicalDay",info);
		} finally{
		if(session != null)
		session.close();
		}
		return periodicalDayList;
	}

	@Override
	public void update_checkIn(AttendInfo attendanceInfo)
			throws RecordNotFoundException {
		System.out.println("출석업데이트");
		if(!isExist(attendanceInfo.getCustId(),attendanceInfo.getCourseId(),attendanceInfo.getDay())) throw new RecordNotFoundException(attendanceInfo);
		SqlSession session = db.getSession();
		try{
			session.update("dbConfig.AttendInfoMapper.update_checkIn", attendanceInfo);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}
		
	}

	@Override
	public void update_checkOut(AttendInfo attendanceInfo)
			throws RecordNotFoundException {
		System.out.println("퇴실업데이트");
		if(!isExist(attendanceInfo.getCustId(),attendanceInfo.getCourseId(),attendanceInfo.getDay())) throw new RecordNotFoundException(attendanceInfo);
		SqlSession session = db.getSession();
		try{
			session.update("dbConfig.AttendInfoMapper.update_checkOut", attendanceInfo);
			session.commit();
		}finally{
			if(session != null)
			session.close();
		}
		
	}

	@Override
	public ArrayList<AttendInfo> getAllPeriodAttendInfoList(int courseId) {
		ArrayList<AttendInfo> attendInfoList = new ArrayList<AttendInfo>();
		SqlSession session = db.getSession();
		try{
			attendInfoList = (ArrayList) session.selectList("dbConfig.AttendInfoMapper.getAllPeriodAttendInfoList",courseId);
		} finally{
		if(session != null)
		session.close();
		}
		return attendInfoList;
	}

	@Override
	public List getMonthCustId(int courseId,String month) {
		SqlSession session = db.getSession();
		List custIdList = null;
		HashMap info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("month", month);
		try{
			custIdList = session.selectList("dbConfig.AttendInfoMapper.getMonthCustId",info);
		} finally{
		if(session != null)
		session.close();
		}
		return custIdList;
	}
	public List getAllCustId(int courseId) {
		SqlSession session = db.getSession();
		List custIdList = null;
		try{
			custIdList = session.selectList("dbConfig.AttendInfoMapper.getAllCustId",courseId);
		} finally{
		if(session != null)
		session.close();
		}
		return custIdList;
	}

	@Override
	public List getMonthList(int courseId, String startDate,
			String endDate) {
		SqlSession session = db.getSession();
		List monthList = null;
		HashMap info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("startDate", startDate);
		info.put("endDate", endDate);
		try{
			monthList = session.selectList("dbConfig.AttendInfoMapper.getMonthList",info);
		} finally{
		if(session != null)
		session.close();
		}
		return monthList;
	}

	@Override
	public ArrayList<AttendInfo> getMonthAttendInfoList(int courseId,
			String month) {
		ArrayList<AttendInfo> attendInfoList = new ArrayList<AttendInfo>();
		SqlSession session = db.getSession();
		
		HashMap info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("month", month);
		
		try{
			attendInfoList = (ArrayList) session.selectList("dbConfig.AttendInfoMapper.getMonthAttendInfoList",info);
		} finally{
		if(session != null)
		session.close();
		}
		return attendInfoList;
	}


}
